// 微博爬虫系统 MongoDB 初始化脚本

// 切换到应用数据库
db = db.getSiblingDB('weibo_crawler');

// 创建集合和索引

// 微博帖子集合
db.createCollection('weibo_posts');
db.weibo_posts.createIndex({ "mid": 1 }, { unique: true });
db.weibo_posts.createIndex({ "uid": 1 });
db.weibo_posts.createIndex({ "created_at": -1 });
db.weibo_posts.createIndex({ "crawled_at": -1 });
db.weibo_posts.createIndex({ "topics": 1 });
db.weibo_posts.createIndex({ "mentions": 1 });
db.weibo_posts.createIndex({ "text": "text" }); // 全文搜索索引

// 微博评论集合
db.createCollection('weibo_comments');
db.weibo_comments.createIndex({ "cid": 1 }, { unique: true });
db.weibo_comments.createIndex({ "mid": 1 });
db.weibo_comments.createIndex({ "uid": 1 });
db.weibo_comments.createIndex({ "reply_to": 1 });
db.weibo_comments.createIndex({ "created_at": -1 });
db.weibo_comments.createIndex({ "crawled_at": -1 });
db.weibo_comments.createIndex({ "text": "text" }); // 全文搜索索引

// 微博用户集合
db.createCollection('weibo_users');
db.weibo_users.createIndex({ "uid": 1 }, { unique: true });
db.weibo_users.createIndex({ "profile.screen_name": 1 });
db.weibo_users.createIndex({ "crawled_at": -1 });
db.weibo_users.createIndex({ "profile.verified": 1 });
db.weibo_users.createIndex({ "stats.followers_count": -1 });

// 媒体引用集合
db.createCollection('media_refs');
db.media_refs.createIndex({ "mid": 1 });
db.media_refs.createIndex({ "cid": 1 });
db.media_refs.createIndex({ "hash": 1 }, { unique: true });
db.media_refs.createIndex({ "url": 1 });
db.media_refs.createIndex({ "created_at": -1 });

// 原始页面数据集合（用于调试和重新解析）
db.createCollection('raw_pages');
db.raw_pages.createIndex({ "url": 1 });
db.raw_pages.createIndex({ "page_type": 1 });
db.raw_pages.createIndex({ "crawled_at": -1 });

// 创建 TTL 索引用于自动清理过期数据
// 原始页面数据保留 30 天
db.raw_pages.createIndex({ "crawled_at": 1 }, { expireAfterSeconds: 2592000 });

// 插入示例数据结构文档（用于参考）
db.weibo_posts.insertOne({
  "_id": ObjectId(),
  "mid": "example_mid_123456789",
  "uid": "example_uid_123456789",
  "text": "这是一条示例微博内容 #示例话题# @示例用户",
  "topics": ["示例话题"],
  "mentions": ["示例用户"],
  "stats": {
    "reposts_count": 0,
    "comments_count": 0,
    "likes_count": 0
  },
  "media": [],
  "created_at": new Date(),
  "crawled_at": new Date(),
  "raw": {
    "html": "<div>原始HTML内容</div>",
    "json": {}
  },
  "_example": true
});

db.weibo_comments.insertOne({
  "_id": ObjectId(),
  "cid": "example_cid_123456789",
  "mid": "example_mid_123456789",
  "uid": "example_uid_123456789",
  "text": "这是一条示例评论",
  "reply_to": null,
  "created_at": new Date(),
  "crawled_at": new Date(),
  "raw": {
    "html": "<div>原始HTML内容</div>",
    "json": {}
  },
  "_example": true
});

db.weibo_users.insertOne({
  "_id": ObjectId(),
  "uid": "example_uid_123456789",
  "profile": {
    "screen_name": "示例用户",
    "name": "示例用户真实姓名",
    "avatar_url": "https://example.com/avatar.jpg",
    "description": "这是用户简介",
    "verified": false,
    "verified_type": null,
    "location": "北京",
    "url": "https://example.com"
  },
  "stats": {
    "followers_count": 1000,
    "followings_count": 500,
    "posts_count": 2000
  },
  "crawled_at": new Date(),
  "raw": {
    "html": "<div>原始HTML内容</div>",
    "json": {}
  },
  "_example": true
});

// 删除示例数据
db.weibo_posts.deleteMany({ "_example": true });
db.weibo_comments.deleteMany({ "_example": true });
db.weibo_users.deleteMany({ "_example": true });

print("MongoDB 初始化完成！");
print("已创建集合：weibo_posts, weibo_comments, weibo_users, media_refs, raw_pages");
print("已创建相关索引用于优化查询性能");
